/**
 * Created by zhangyi on 2017/11/6.
 */
mainApp.factory("empLeaveFactory",function(){

    var service={};

    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 6
        }
        var list = "";

        RequestUtils.postJson('../employee/getByStatus/0',obj).done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                console.log(resp.data);
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });

        return list;
    }
    service.getAllByEmpIdOrName=function(employee_id,name){
        console.log(employee_id);
        console.log(name);
        var list = "";
        $.ajax({
            url: '../employee/get/' + employee_id + '/' + name,
            type: 'GET',
            async: 'true',
            cache: 'no-cache',
            dataType: 'json',
            async: false,//同步
            success: function (result) {
                list=result.data;
                console.log(list);
            }

        })
        console.log(list);
        return list;
    }
    service.getEmpById=function(id){
        var list = "";
        $.ajax({

            url: '../employee/getById/' + id,

            type: 'GET',
            async: 'true',
            cache: 'no-cache',
            dataType: 'json',
            async: false,//同步
            success: function (result) {
                list=result.data;
            }
        })

        return list;
    }


    service.addEmp=function(empObj){
        empObj.photo=empObj.jobnum+'.jpg';
        empObj.pwd='123';
        empObj.status='0';
        var names = [];
        console.log(empObj);
        names.push({name:empObj.photo })
        RequestUtils.fileUpload('../employee/loadphoto',['newPhotoFile'], names);
        RequestUtils.postForm('../employee/add',empObj).done(function (resp) {
            if(resp.resultCode==0){
                CommonUtils.buildFailHint("新增数据","成功");
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('新增数据', '失败');
            }
        });
    }


    service.UpdateEmp=function(empObj){
        console.log(empObj);
        RequestUtils.postForm('../employee/update/', empObj)
            .done(function (resp) {
                if(resp.resultCode==0){

                    alert("修改成功");
                    location.reload(true);

                }else{
                    alert("失败")
                }

            });
    }

    return service;
})

mainApp.controller("empLeaveController",function($scope,$http,empLeaveFactory){
    $scope.title="离职员工";
    $scope.list="";
    $scope.dateTime=CommonUtils.getDateTime();
    console.log($scope.dateTime);



    //增加模态框里的数据绑定
    $scope.addEmpData;


    //获取数据 全部的
    $scope.list=empLeaveFactory.getListData(1);
    $scope.emplist=$scope.list.items;
    console.log($scope.emplist);

    //查询数据 姓名或者工号
    $scope.empWhere;
    $scope.selectEmpByWhere=function(empWhere){
        if($("#selectEmpJobnum").val()==''||$("#selectEmpJobnum").val()==null){
            empWhere.jobnum='null';
        }
        if($("#selectEmpName").val()==''||$("#selectEmpName").val()==null){
            empWhere.name='null';
        }
        $scope.emplist=empLeaveFactory.getAllByEmpIdOrName(empWhere.jobnum,empWhere.name);
        console.log($scope.emplist);
        empWhere.jobnum="";
        empWhere.name="";
    }


    //增加
    $scope.emp;
    $scope.addEmp=function(emp){

        var empObj = {
            'number': emp.number,
            'jobnum': emp.jobnum,
            'name': emp.name,
            'departId': emp.departId,
            'post': emp.post,
            'phone': emp.phone,
            'd_name': emp.d_name,
            'd_phone': emp.d_phone,
            'd_email': emp.d_email,
            'status': emp.status,
            'information':emp.information
        };
        console.log(empObj);
        //增加
        empLeaveFactory.addEmp(empObj);

        $("#addEmpModal").modal("hide");
        $scope.$apply();//需要手动刷新
    }


    $scope.Emp;
    //查询单人
    $scope.selectEmp=function(id){
        var emp=empLeaveFactory.getEmpById(id);

        $scope.Emp=emp;

        $("#EmpModal").modal("show");

    }
    //更新按钮
    $scope.Update=function(emp){
        var empObj = {
            'date_of_birth': emp.date_of_birth,
            'employee_id': emp.employee_id,
            'id': emp.id,
            'name': emp.name,
            'phone': emp.phone,
            'photo': emp.photo,
            'place_of_birth': emp.place_of_birth,
            'pwd': emp.pwd,
            'sex': emp.sex,
            'state': emp.state,
            'department':emp.department
        };
        console.log(empObj);
        //更新
        empLeaveFactory.UpdateEmp(empObj);
        $("#EmpModal").modal("hide");
        //重新刷新页面

    }

    $scope.dimission=function(){
        if(window.confirm("确定这位员工离职？")){
            console.log("离职")
        }

    }

    $scope.openAddEmpModal=function(){

        $("#addEmpModal").modal("show");
    }

    $scope.openSelectEmpModal=function(){

        $("#selectEmpModal").modal("show");
    }

    //翻页
    $scope.NextListData=function(pageIndex){
        //分页后的数据
        $scope.list=empLeaveFactory.getListData(pageIndex);
        //每一页的数据
        $scope.emplist=$scope.list.items;

    }

    CommonUtils.setDatetimepicker("datetimepicker");
    CommonUtils.setDatetimepicker("updateDatetimepicker");
})